package org.uugu.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.uugu.model.News;
import org.uugu.service.NewsManager;
import org.uugu.util.PageControl;


@Component("newsAction")
@Scope("prototype")   
public class NewsAction extends BaseAction {  
	private Log log = LogFactory.getLog(NewsAction.class);
    private NewsManager newsManager;
    private List news;
    private Integer newsId;
    private String newsName;
    private String newsType;
    private String newsText;
    private Date newsDate;
    private Integer newsHot;
    private Integer newsItem;
    
/************************根据ID获取某条新闻*************************/
    public String listById(){
    	try{
    		news = this.newsManager.listById(newsId);
    		if(news.size()==1){
    			return "listNew";
    		}else{
    			return "error";
    		}    		
    	}catch(Exception e){
			return "error";
		} 
    }


/**********************后台===根据ID获取某条新闻**********************/
    public String listByIdBack(){
    	try{
    		news = this.newsManager.listById(newsId);
    		if(news.size()==1){
    			if(newsItem==1){
    				return "listNewBack";
    			}else if(newsItem==2){
    				return "listNoteBack";
    			}else if(newsItem==3){
    				return "listVideoBack";
    			}else if(newsItem==4){
    				return "listDonateBack";
    			}else if(newsItem==5){
    				return "listManBack";
    			}else{
    				return "error";
    			}
    		}else{
    			return "error";
    		}    		
    	}catch(Exception e){
			return "error";
		} 
    }
    
    
/************************根据ID删除某条新闻*************************/
    public String delById(){
    	try{
    		String result= this.newsManager.delById(newsId);
    		this.setPage(getPage());
    		this.setPageSize(getPageSize());
    		this.setNewsItem(newsItem);
    		return result;    		
    	}catch(Exception e){
    		e.printStackTrace();
			return "error";
		} 
    }
    
/**************************添加新闻********************************/
    public String addNew() {
    	try{
    		//log.info(newsName+newsType+newsText);
    		if(!newsName.equals("")&&!newsType.equals("")&&!newsText.equals("")){
    		News anew = new News();
    		anew.setNewsName(newsName);
    		anew.setNewsType(newsType);
    		anew.setNewsText(newsText);
    		anew.setNewsDate(new Date());
    		anew.setNewsHot(0);
    		anew.setNewsItem(newsItem);    		
    		String result = this.newsManager.add(anew);
        	return result;
    		}else{
    			return "error";
    		}
    	}catch(Exception e){
    		return "error";
    	}
    } 
    
/**************************更新新闻********************************/
    public String updateNew() {
    	try{
    		//log.info(newsName+newsType+newsText);
    		if(!newsName.equals("")&&!newsType.equals("")&&!newsText.equals("")){
	    		News anew = new News();
	    		anew.setNewsId(newsId);
	    		anew.setNewsName(newsName);
	    		anew.setNewsType(newsType);
	    		anew.setNewsText(newsText);
	    		anew.setNewsHot(newsHot);
	    		anew.setNewsDate(new Date());
	    		anew.setNewsItem(newsItem);    		
	    		String result = this.newsManager.update(anew);
	        	return result;
    		}else{
    			return "error";
    		}
    	}catch(Exception e){
    		return "error";
    	}
    }    
    

	
/******************************后台查询某页数据*******************************/	
	public String listNewsByPage(){  
		try{
			//log.info("================"+getPage()+"=========="+getPageSize());
			PageControl pc =new PageControl();
			pc.setPage(getPage());
			pc.setPageSize(getPageSize());
			news = this.newsManager.listNewsInPage(newsItem,pc);
			this.setPage(pc.getPage());
			this.setPageSize(pc.getPageSize());
			this.setPrePage(pc.getPrePage());
			this.setNextPage(pc.getNextPage());
			this.setEndPage(pc.getEndPage());
			if(newsItem==1){
				return "listNewsBack";
			}else if(newsItem==2){
				return "listNotesBack";
			}else if(newsItem==3){
				return "listVideosBack"; 
			}else if(newsItem==4){
				return "listDonatesBack";
			}else if(newsItem==5){
				return "listMansBack";
			}else{
				return "error";
			}
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		} 
    }
    
/******************************默认执行方法*******************************/	
	@Override
	public String execute() throws Exception {  
		try{
			//log.info("================"+getPage()+"=========="+getPageSize());
			PageControl pc =new PageControl();
			pc.setPage(getPage());
			pc.setPageSize(getPageSize());
			news = this.newsManager.listNewsInPage(newsItem,pc);
			this.setPage(pc.getPage());
			this.setPageSize(pc.getPageSize());
			this.setPrePage(pc.getPrePage());
			this.setNextPage(pc.getNextPage());
			this.setEndPage(pc.getEndPage());
			if(newsItem==1){
				return "listNews";
			}else if(newsItem==2){
				return "listNotes";
			}else if(newsItem==3){
				return "listVideos"; 
			}else if(newsItem==4){
				return "listDonates";
			}else if(newsItem==5){
				return "listMans";
			}else{
				return "error";
			}
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		} 
    } 
	
	
/****************************Getter And Setters***************************/
    public NewsManager getNewsManager() {
		return newsManager;
	}
    @Resource
	public void setNewsManager(NewsManager newsManager) {
		this.newsManager = newsManager;
	}
	
	public List getNews() {
		return news;
	}
	public void setNews(List news) {
		this.news = news;
	}


	public Integer getNewsId() {
		return newsId;
	}

	public void setNewsId(Integer newsId) {
		this.newsId = newsId;
	}

	public String getNewsName() {
		return newsName;
	}

	public void setNewsName(String newsName) {
		this.newsName = newsName;
	}

	public String getNewsType() {
		return newsType;
	}

	public void setNewsType(String newsType) {
		this.newsType = newsType;
	}

	public String getNewsText() {
		return newsText;
	}

	public void setNewsText(String newsText) {
		this.newsText = newsText;
	}

	public Date getNewsDate() {
		return newsDate;
	}

	public void setNewsDate(Date newsDate) {
		this.newsDate = newsDate;
	}

	public Integer getNewsHot() {
		return newsHot;
	}

	public void setNewsHot(Integer newsHot) {
		this.newsHot = newsHot;
	}

	public Integer getNewsItem() {
		return newsItem;
	}

	public void setNewsItem(Integer newsItem) {
		this.newsItem = newsItem;
	}

} 
